package com.itheima.bigevent.controller;

import com.itheima.bigevent.pojo.Result;
import com.itheima.bigevent.pojo.User;
import com.itheima.bigevent.property.JWTProperties;
import com.itheima.bigevent.service.UserService;
import com.itheima.bigevent.utils.JwtUtil;
import com.itheima.bigevent.utils.Md5Util;
import com.itheima.bigevent.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Pattern;
import org.apache.ibatis.javassist.expr.NewArray;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: UserController
 * Package: com.itheima.bigevent.controller
 * Description:用户的controller层
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-08-19-14:55
 * @Version: v1.0
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate rds;
    @Resource
    private JWTProperties jwtProperties;

    /**
     * 用户注册接口
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/register")
    // 使用Validation 对注册接口的参数进行合法性验证
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,
                           @Pattern(regexp = "^\\S{5,16}$") String password){
        // 查询用户
        User user = userService.findByUserName(username);
        // 判断
        if(null == user){
            // 用户不存在
            userService.register(username, password);
            return Result.success();
        } else{
            return Result.error("用户名已存在");
        }
    }

    /**
     * 用户登录接口
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username,
                                @Pattern(regexp = "^\\S{5,16}$") String password){
        // 查询用户
        User user = userService.findByUserName(username);
        // 判断
        if(null == user){
            // 用户不存在
            return Result.error("用户名不存在");
        } else{
            // 判断密码
            if(Md5Util.getMD5String(password).equals(user.getPassword())){
                // 生成token秘钥
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", user.getId());
                map.put("username", user.getUsername());
                // 使用工具类生成token
                String token = JwtUtil.genToken(map, jwtProperties.getTtl(), jwtProperties.getSecretKey());
                // 将token存储在redis中
                rds.opsForValue().set("token_" + user.getId(), token, 1, TimeUnit.HOURS);
                // 将token返回
                return Result.success(token);
            } else{
                return Result.error("密码错误");
            }
        }
    }

    /**
     * 获取用户信息接口
     * @return
     */
    @GetMapping("/userInfo")
    public Result<User> userInfo(){
        // 返回存储在ThreadLocalUtil中的map
        Map<String, Object> map = ThreadLocalUtil.get();
        // 查询用户
        User user = userService.findByUserName((String) map.get("username"));
        // 返回用户信息
        return Result.success(user);
    }

    /**
     * 更新用户简要信息接口
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        // 更新用户信息
        userService.update(user);
        return Result.success();
    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam(value = "avatarUrl") @URL String avatarUrl){
        // 更新用户头像
        userService.updateAvatar(avatarUrl);
        // 返回成功
        return Result.success();
    }

    /**
     * 更新用户密码接口
     * @param map
     * @return
     */
    @PatchMapping("/updatePassword")
    public Result<String> updatePassword(@RequestBody Map<String, Object> map){
        return userService.updatePassword(map);
    }

}
